import com.alibaba.druid.pool.DruidDataSource;
import com.xff.bean.Book;
import com.xff.bean.Login;
import com.xff.bean.Person;
import com.xff.controller.IpersonController;
import com.xff.controller.PersonController;
import com.xff.service.LoginService;
import com.xff.service.MutilService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/*
* 测试的方法不可以有参数和返回值
* */
public class MyJunit {

    @Test
    public void test01() throws NoSuchMethodException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ioc.xml");
//        IpersonController personController = context.getBean(IpersonController.class);
//        personController.save(10,2);
        IpersonController personController1 = (IpersonController) context.getBean("personController",IpersonController.class);

//        personController1.save(12,10);
        System.out.println(personController1.getClass());


    }

    @Test
    public void test02() throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource dataSource = applicationContext.getBean("dataSource", DruidDataSource.class);
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

        String sql="insert into login(id,username,password) values(?,?,?)";
        int a = jdbcTemplate.update(sql,4,"lisi","1589");
        System.out.println(a);
    }

    @Test
    public void test03() throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource dataSource = applicationContext.getBean("dataSource", DruidDataSource.class);
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

        String sql="insert into login(id,username,password) values(?,?,?)";
        List<Object[]> list=new ArrayList<>();
        list.add(new Object[]{5,"wangwu","789"});
        list.add(new Object[]{6,"maliu","123"});
        int[] a = jdbcTemplate.batchUpdate(sql,list);
        for (int i : a) {
            System.out.println(i);
        }
    }

    @Test
    public void test04() throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

        String sql="delete from login where id=?";
        int a = jdbcTemplate.update(sql,4);
        System.out.println(a);
    }

    @Test
    public void test05() throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

        String sql="update login set username=? where id=?";
        int a = jdbcTemplate.update(sql,"lisi","5");
        System.out.println(a);
    }

    @Test
    public void test06() throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

        String sql="select * from login where id>?";
        List<Login> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Login.class), 0);
        for (Login login : result) {
            System.out.println(login);
        }
    }

    @Test
    public void test07() throws SQLException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

        String sql="select * from login where id=?";
        Login result = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Login.class), 1);
        System.out.println(result);
    }

    @Test
    public void test08() throws SQLException, FileNotFoundException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        LoginService loginService = applicationContext.getBean("loginService", LoginService.class);
        loginService.update();


    }

    @Test
    public void test09(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        MutilService mutilService = applicationContext.getBean("mutilService", MutilService.class);

        mutilService.mutilupdate();
    }


}
